export default class GUIGroup {
	constructor(parent) {
		this.parent = parent ;
		this.isGroup = true ;

		this.children = [];

		//添加到该级的controller
		this.controllers = [] ;
		//folder
		this.folders = [] ;
		//fieldset
		this.fieldsets = [] ;

		//是否隐藏状态
		this._hidden = false ;

		this._disabled = false ;

		if(parent != null && parent.isGroup === true){
			parent.addChild(this) ;
		}
	}

	addChild(group){
		if(group == null) return this;
		if(group.isGroup !== true) return this;
		this.children.push(group) ;
		return this ;
	}

	addController(controller){
		if(controller == null) return this;
		if(controller.isController !== true) return this;
		this.controllers.push(controller) ;
		return this ;
	}

	addFieldset(fieldset){
		if(fieldset == null) return this;
		if(fieldset.isFieldset !== true) return this;
		this.fieldsets.push(fieldset) ;
		return this ;
	}

	addFolder(folder){
		if(folder == null) return this;
		if(folder.isFolder !== true) return this;
		this.folders.push(folder) ;
		return this ;
	}

	updateDisplay(){
		for(let i=0; i<this.controllers.length; i++){
			let child = this.controllers[i] ;
			child.updateDisplay() ;
		}
		return this ;
	}

	show( show = true ){
		//父节点隐藏，子节点也隐藏
		if(show == true && this.findParentIsHidden() == true){
			show = false ;
		}
		this._hidden = !show;

		for(let i=0; i<this.controllers.length; i++){
			let child = this.controllers[i] ;
			child.show(show) ;
		}

		for(let i=0; i<this.fieldsets.length; i++){
			let child = this.fieldsets[i] ;
			child.show(show) ;
		}

		return this ;
	}

	showAll(show = true){
		this.show(show) ;
		for(let i=0; i<this.children.length; i++){
			let child = this.children[i] ;
			child.showAll(show) ;
		}
	}

	disable( disabled = true ) {
		//父节点禁用，子节点也禁用
		if(disabled == false && this.findParentIsDisabled() == true){
			disabled = true ;
		}

		this._disabled = disabled;

		for(let i=0; i<this.controllers.length; i++){
			let child = this.controllers[i] ;
			child.disable(disabled) ;
		}

		return this ;
	}

	readonly(flag = true){
		//父节点只读，子节点也只读
		if(flag == false && this.findParentIsReadonly() == true){
			flag = true ;
		}
		this._readonly = flag;

		for(let i=0; i<this.controllers.length; i++){
			let child = this.controllers[i] ;
			child.readonly(flag) ;
		}

		return this ;
	}

	findParentIsHidden(){
		if(this.parent == null) return false ;
		let obj = this ;
		while(obj.parent != null){
			obj = obj.parent ;
			if(obj._hidden == true) return true ;
		}
		return false ;
	}

	findParentIsDisabled(){
		if(this.parent == null) return false ;
		let obj = this ;
		while(obj.parent != null){
			obj = obj.parent ;
			if(obj._disabled == true) return true ;
		}
		return false ;
	}

	findParentIsReadonly(){
		if(this.parent == null) return false ;
		let obj = this ;
		while(obj.parent != null){
			obj = obj.parent ;
			if(obj._readonly == true) return true ;
		}
		return false ;
	}

	destroy(){
		for(let i=this.controllers.length-1; i>=0; i--){
			this.controllers[i].destroy() ;
		}
		this.controllers = [] ;
		for(let i=this.children.length-1; i>=0; i--){
			this.children[i].destroy() ;
		}
		this.children = [] ;

		for(let i=this.fieldsets.length-1; i>=0; i--){
			this.fieldsets[i].destroy() ;
		}
		this.fieldsets = [] ;

		this.destroyFolder() ;
		return this ;
	}

	destroyFolder(){
		for(let i=this.folders.length-1; i>=0; i--){
			this.folders[i].destroy() ;
		}
		this.folders = [] ;
		return this ;
	}

	removeController(controller){
		if(controller == null) return this ;
		if(controller.isController !== true) return this ;
		for(let i=0; i<this.controllers.length; i++){
			if(this.controllers[i] == controller){
				this.controllers.splice(i,1) ;
				break ;
			}
		}
		return this ;
	}

	removeFolder(folder){
		if(folder == null) return this ;
		if(folder.isFolder !== true) return this ;
		for(let i=0; i<this.folders.length; i++){
			if(this.folders[i] == folder){
				this.folders.splice(i,1) ;
				break ;
			}
		}
		return this ;
	}
}
